<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.smartaccounting.dao.IncomeDAO" %>
<%@ page import="com.smartaccounting.dao.ExpenseDAO" %>
<%@ page import="com.smartaccounting.dao.BudgetDAO" %>
<%@ page import="com.smartaccounting.model.User" %>
<%@ page import="java.math.BigDecimal" %>
<%
  // 检查登录
  if (session.getAttribute("user") == null) {
    response.sendRedirect("../login.jsp");
    return;
  }

  User user = (User) session.getAttribute("user");
  IncomeDAO incomeDAO = new IncomeDAO();
  ExpenseDAO expenseDAO = new ExpenseDAO();
  BudgetDAO budgetDAO = new BudgetDAO();

  // 获取当前月份
  java.time.LocalDate now = java.time.LocalDate.now();
  String currentMonth = now.getYear() + "-" + String.format("%02d", now.getMonthValue());

  BigDecimal totalIncome = BigDecimal.ZERO;
  BigDecimal totalExpense = BigDecimal.ZERO;
  BigDecimal totalBudget = BigDecimal.ZERO;

  try {
    totalIncome = incomeDAO.getTotalIncomeByUserIdAndMonth(user.getUserId(), currentMonth);
    totalExpense = expenseDAO.getTotalExpenseByUserIdAndMonth(user.getUserId(), currentMonth);
    totalBudget = budgetDAO.getTotalBudgetByUserIdAndMonth(user.getUserId(), currentMonth);
  } catch (Exception e) {
    e.printStackTrace();
  }

  BigDecimal balance = totalIncome.subtract(totalExpense);
  BigDecimal savingsRate = totalIncome.compareTo(BigDecimal.ZERO) > 0 ?
          balance.divide(totalIncome, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)) :
          BigDecimal.ZERO;
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>用户仪表板 - 智能记账系统</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body>
<!-- 导航栏和侧边栏代码保持不变 -->

<!-- 统计卡片 - 使用真实数据 -->
<div class="row">
  <div class="col-xl-3 col-md-6 mb-4">
    <div class="card border-left-primary shadow h-100 py-2 stat-card">
      <div class="card-body">
        <div class="row no-gutters align-items-center">
          <div class="col mr-2">
            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">
              本月收入</div>
            <div class="h5 mb-0 font-weight-bold text-gray-800">¥<%= totalIncome %></div>
          </div>
          <div class="col-auto">
            <i class="fas fa-money-bill-wave fa-2x text-gray-300"></i>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-xl-3 col-md-6 mb-4">
    <div class="card border-left-success shadow h-100 py-2 stat-card">
      <div class="card-body">
        <div class="row no-gutters align-items-center">
          <div class="col mr-2">
            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">
              本月支出</div>
            <div class="h5 mb-0 font-weight-bold text-gray-800">¥<%= totalExpense %></div>
          </div>
          <div class="col-auto">
            <i class="fas fa-shopping-cart fa-2x text-gray-300"></i>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-xl-3 col-md-6 mb-4">
    <div class="card border-left-warning shadow h-100 py-2 stat-card">
      <div class="card-body">
        <div class="row no-gutters align-items-center">
          <div class="col mr-2">
            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">
              结余金额</div>
            <div class="h5 mb-0 font-weight-bold text-gray-800">¥<%= balance %></div>
          </div>
          <div class="col-auto">
            <i class="fas fa-wallet fa-2x text-gray-300"></i>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-xl-3 col-md-6 mb-4">
    <div class="card border-left-info shadow h-100 py-2 stat-card">
      <div class="card-body">
        <div class="row no-gutters align-items-center">
          <div class="col mr-2">
            <div class="text-xs font-weight-bold text-info text-uppercase mb-1">
              储蓄率</div>
            <div class="h5 mb-0 font-weight-bold text-gray-800"><%= savingsRate %>%</div>
          </div>
          <div class="col-auto">
            <i class="fas fa-piggy-bank fa-2x text-gray-300"></i>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>